import 'dart:async';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/widget/_.dart';
import 'package:thinking/widget/WaterFallFlow/WaterFallFlow.dart';
import 'package:thinking/utils/_.dart';


class DefaultLayout extends StatefulWidget{

  @override
  _Default createState() =>_Default();

}

class _Default extends State<DefaultLayout> with  AutomaticKeepAliveClientMixin<DefaultLayout>{

  @override
  bool get wantKeepAlive => true;

  HomePage homePage= HomePage();
  List<Think> thinkData = [];
  List<BannerItem> bannerData = [];
  bool isGetHomeData = false;
  late EasyRefreshController _easyRefreshController;
  late PageController _pageController;


    @override
  void initState() {
    super.initState();
    _pageController = PageController();
    getDefaultData(isShowHttpLoading : false);
    _easyRefreshController = EasyRefreshController();

  }


  ///请求推荐页数据
  Future<void> getDefaultData({required bool isShowHttpLoading,bool isOnRefresh:false,bool isOnLoad:false}) async{
    HomeResult? res =  await HomeService.getHomeData(params:
    {
      'think_Page':homePage.thinkPage,
      'think_Count':homePage.thinkCount,
      'isShowHttpLoading':isShowHttpLoading,
      "user_Id": SPreferences().getInt('user_Id'),
      "banner_type":0

    });
    bannerData = res!.bannerData!;
    if(homePage.canThink){
      homePage.setThinkPage = homePage.thinkPage +1;
    }
    if(res.thinkData!.length<homePage.thinkCount){
        homePage.setCanThink = false;
    }


      setState(() {
        if(isOnRefresh ){
          thinkData = [...?res.thinkData,...thinkData];
          if(res.thinkData!.length>0) BotToast.showText(text: '更新了${res.thinkData!.length}条数据');
          if(!homePage.canThink)   BotToast.showText(text: '没有更多think了哦~');

        }else{
          thinkData = [...thinkData,...?res.thinkData];
          if(res.thinkData!.length>0 && isOnLoad) BotToast.showText(text: '更新了${res.thinkData!.length}条数据');
          if(!homePage.canThink && isOnLoad)   BotToast.showText(text: '已经到底了哦~');

        }
        this.isGetHomeData = true;
      });




  }



  Widget easyRefresh(Widget child){

      return EasyRefresh(
        child: child,
        header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
        footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        controller: _easyRefreshController,
        bottomBouncing: true,
        onRefresh: () async {
          if(!this.isGetHomeData)return;
          await getDefaultData(isShowHttpLoading: false,isOnRefresh: true);
          _easyRefreshController.finishRefresh(success: true);
        },
        onLoad: () async {
          if(!this.isGetHomeData)return;
          await getDefaultData(isShowHttpLoading: false,isOnLoad: true);
          _easyRefreshController.finishLoad(success: true);
        },
      );

  }


  @override
  void dispose() {
    _easyRefreshController.dispose();
    _pageController.dispose();
    super.dispose();
  }

    @override
  Widget build(BuildContext context) {
      super.build(context);
      return this.isGetHomeData? Empty(
        isShow: thinkData.length == 0,
        child:
              easyRefresh(ListView(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                children: [
                  SizedBox(height: Screen.setHeight(10),),
                  ConstrainedBox(
                      child: BannerWidget(
                        bannerData: bannerData,
                      ),

                      constraints:BoxConstraints.loose(Size.fromHeight(170))
                  ),
                  SizedBox(height: Screen.setHeight(10),),
                  Split(splitData: 'think'),
                  SizedBox(height: Screen.setHeight(10),),
                  SpannableCountCountPage(thinkDataList: thinkData)
                ],
              ))
      ):Loading();

  }

}

